/*
 * GuiDisplaySearchFind.java
 *
 * Created on 20 August 2003, 09:10
 * Copyright (C) 2003, Velocityme Partnership
 */

package com.velocityme.client.gui;

import com.velocityme.client.gui.calendar.CalendarDatePanel;
import com.velocityme.client.gui.exception.ExceptionWindow;
import java.util.*;
import javax.ejb.*;
import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import java.awt.Component;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import javax.swing.JTabbedPane;
import com.velocityme.client.gui.exception.*;
import com.velocityme.client.gui.node.contactable.ContactableTreeCellRenderer;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.utility.InvalidKeyException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author  Robert Crida Work
 */
public class GuiDisplaySearchParameters extends javax.swing.JPanel {
    
    private NodePK m_rootNodePK;
    private KeySession m_key;
    private ValueListModel m_statuses;
    private TreeModel m_contactableTreeModel;
    private CalendarDatePanel m_startDatePanel;
    private CalendarDatePanel m_stopDatePanel;
    
    /** Creates new form GuiDisplaySearchFind */
    public GuiDisplaySearchParameters(KeySession p_key, NodePK p_rootNodePK) {
        m_key = p_key;
        m_rootNodePK = p_rootNodePK;
        try {
            m_statuses = new ValueListModel(StatelessRemoteClientSessionUtil.getSession().getAllStatusValues(m_key));
            m_contactableTreeModel = StatelessRemoteClientSessionUtil.getSession().getContactableValueTree(m_key);
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
        initComponents();
        try {
            m_startDatePanel = new CalendarDatePanel();
            m_stopDatePanel = new CalendarDatePanel();
            java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 2;
            gridBagConstraints.gridy = 3;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
            jPanel2.add(m_startDatePanel, gridBagConstraints);
            gridBagConstraints.gridy = 4;
            jPanel2.add(m_stopDatePanel, gridBagConstraints);
            Date now = new Date();
            m_startDatePanel.setDate(now);
            m_stopDatePanel.setDate(now);
        
            jTextFieldRoot.setText(StatelessRemoteClientSessionUtil.getSession().getNodePathName(m_key, m_rootNodePK));
            jTreeContactable.setModel(m_contactableTreeModel);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
    }
    
    private Integer[] getIds() {
        if (jTextFieldIds.getText().length() > 0) {
            String[] numbers = Pattern.compile("\\D+").split(jTextFieldIds.getText());
            Integer[] ids = new Integer[numbers.length];
            for (int i = 0; i < numbers.length; i++) {
                ids[i] = Integer.decode(numbers[i]);
            }
            return ids;
        }
        else
            return new Integer[0];
    }
    
    private String[] getKeywords() {
        if (jTextFieldKeywords.getText().length() > 0) {
            return Pattern.compile("\\s+").split(jTextFieldKeywords.getText());
        }
        else
            return new String[0];
    }
    
    private StatusPK[] getStatusPks() {
        Object[] objects = jListStatus.getSelectedValues();
        StatusPK[] pks = new StatusPK[objects.length];
        for (int i = 0; i < objects.length; i++) {
            pks[i] = ((StatusValue)objects[i]).getPrimaryKey();
        }
        return pks;
    }
    
    private ContactablePK[] getContactablePks() {
        TreePath[] addPaths = jTreeContactable.getSelectionPaths();
        ContactablePK[] pks;
        if (addPaths != null) {
            pks = new ContactablePK[addPaths.length];
            for (int i = 0; i < addPaths.length; i++) {
                pks[i] = ((ContactableValue)((DefaultMutableTreeNode)addPaths[i].getLastPathComponent()).getUserObject()).getPrimaryKey();
            }
        }
        else
            pks = new ContactablePK[0];
        return pks;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel2 = new javax.swing.JPanel();
        jLabelRoot = new javax.swing.JLabel();
        jTextFieldRoot = new javax.swing.JTextField();
        jLabelIds = new javax.swing.JLabel();
        jTextFieldIds = new javax.swing.JTextField();
        jLabelKeywords = new javax.swing.JLabel();
        jTextFieldKeywords = new javax.swing.JTextField();
        jLabelStart = new javax.swing.JLabel();
        jCheckBoxStart = new javax.swing.JCheckBox();
        jLabelStop = new javax.swing.JLabel();
        jCheckBoxStop = new javax.swing.JCheckBox();
        jLabelStatuses = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListStatus = new javax.swing.JList(m_statuses);
        jLabelContactables = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTreeContactable = new javax.swing.JTree(m_contactableTreeModel);
        jTreeContactable.setCellRenderer(new ContactableTreeCellRenderer());
        jPanel3 = new javax.swing.JPanel();
        jButtonSearch = new javax.swing.JButton();
        jButtonClose = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jPanel2.setMinimumSize(new java.awt.Dimension(0, 0));
        jLabelRoot.setText("Search Root:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jLabelRoot, gridBagConstraints);

        jTextFieldRoot.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jTextFieldRoot, gridBagConstraints);

        jLabelIds.setText("Ids:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jLabelIds, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jTextFieldIds, gridBagConstraints);

        jLabelKeywords.setText("Keywords:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jLabelKeywords, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jTextFieldKeywords, gridBagConstraints);

        jLabelStart.setText("Start:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jLabelStart, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jCheckBoxStart, gridBagConstraints);

        jLabelStop.setText("Stop:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jLabelStop, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jCheckBoxStop, gridBagConstraints);

        jLabelStatuses.setText("Statuses:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jLabelStatuses, gridBagConstraints);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(3, 120));
        jScrollPane1.setViewportView(jListStatus);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jScrollPane1, gridBagConstraints);

        jLabelContactables.setText("Contactables:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jLabelContactables, gridBagConstraints);

        jScrollPane2.setPreferredSize(new java.awt.Dimension(3, 120));
        jTreeContactable.setRootVisible(false);
        jTreeContactable.setShowsRootHandles(true);
        jScrollPane2.setViewportView(jTreeContactable);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel2.add(jScrollPane2, gridBagConstraints);

        add(jPanel2, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.GridLayout(1, 0));

        jButtonSearch.setText("Search");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        jPanel3.add(jButtonSearch);

        jButtonClose.setText("Close");
        jButtonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCloseActionPerformed(evt);
            }
        });

        jPanel3.add(jButtonClose);

        add(jPanel3, java.awt.BorderLayout.SOUTH);

    }//GEN-END:initComponents

    private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
        // Add your handling code here:
        getParent().remove(this);        
    }//GEN-LAST:event_jButtonCloseActionPerformed

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        // Add your handling code here:
        try {
            Integer[] ids = getIds();
            String[] keywords = getKeywords();
            Date start = jCheckBoxStart.isSelected()? m_startDatePanel.getDate(): null;
            Date stop = jCheckBoxStop.isSelected()? m_stopDatePanel.getDate(): null;
            if (stop != null) {
                long time = stop.getTime();
                stop.setTime(time + 999 + 59*1000 + 59*60*1000 + 23*60*60*1000); // Make time 23h59:59.999
            }
            StatusPK[] statusPks = getStatusPks();
            ContactablePK[] contactablePks = getContactablePks();
            Collection results = StatelessRemoteClientSessionUtil.getSession().findParameterSearch(m_key, m_rootNodePK, ids, keywords, start, stop, statusPks, contactablePks);
            Component component = new GuiDisplaySearchResults (m_key, m_rootNodePK, results);
            JTabbedPane tabbedPane = (JTabbedPane)getParent();
            tabbedPane.addTab("Search Results", component);
            tabbedPane.setSelectedComponent(component);
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
    }//GEN-LAST:event_jButtonSearchActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClose;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JCheckBox jCheckBoxStart;
    private javax.swing.JCheckBox jCheckBoxStop;
    private javax.swing.JLabel jLabelContactables;
    private javax.swing.JLabel jLabelIds;
    private javax.swing.JLabel jLabelKeywords;
    private javax.swing.JLabel jLabelRoot;
    private javax.swing.JLabel jLabelStart;
    private javax.swing.JLabel jLabelStatuses;
    private javax.swing.JLabel jLabelStop;
    private javax.swing.JList jListStatus;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextFieldIds;
    private javax.swing.JTextField jTextFieldKeywords;
    private javax.swing.JTextField jTextFieldRoot;
    private javax.swing.JTree jTreeContactable;
    // End of variables declaration//GEN-END:variables
    
}
